package com.danco.training.bookshop.test;

import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;

import com.danco.training.bookshop.entity.Book;
import com.danco.training.bookshop.entity.Claim;
import com.danco.training.bookshop.entity.Order;
import com.danco.training.bookshop.entity.storage.BookStorage;
import com.danco.training.bookshop.entity.storage.Bookshop;
import com.danco.training.bookshop.entity.storage.ClaimStorage;
import com.danco.training.bookshop.entity.storage.OrderStorage;
import com.danco.training.bookshop.service.file.BookStorageFile;
import com.danco.training.bookshop.service.file.ClaimStorageFile;
import com.danco.training.bookshop.service.file.OrderStorageFile;
import com.danco.training.bookshop.service.storage.BookStorageService;
import com.danco.training.bookshop.service.storage.ClaimStorageService;
import com.danco.training.bookshop.service.storage.OrderStorageService;


public class IOClass {

	public static void main(String s[]) throws Exception {

		BookStorage bookStorage = BookStorage.getBookStorageInstance();
		ClaimStorage claimStorage = ClaimStorage.getClaimStorageInstance();
		OrderStorage orderStorage = OrderStorage.getOrderStorageInstance();
			
		BookStorageService bookStorageService = BookStorageService.getBookStorageSeviceInstance();
		OrderStorageService orderStorageService = OrderStorageService.getOrderStorageServiceInstance();
		ClaimStorageService claimStorageService = ClaimStorageService.getClaimStorageServiceInstance();
		
		Date d1 = new Date (1998 - 9 - 05);
		Book book1 = new Book(1, "The Great Gatsby", "F. Scott Fitzgerald", d1,
				56.9, d1);
		Book book2 = new Book(2, "Harry Potter and the Sorcerer's Stone",
				"J.K. Rowling", d1, 76.8, d1);
		Book book3 = new Book(3, "If You've Got It, Haunt It", "Rose Pressey",d1,
				90, d1);
		Book book4 = new Book(
			4, "A Tapestry of Queens: A Story of Scotland's Struggle for Independence",
				"Carol Milkuhn", d1, 78, d1);
		Claim claim = new Claim(1, book4, 2);
	
		bookStorageService.addBook(book1);
		bookStorageService.addBook(book2);
		bookStorageService.addBook(book3);
		bookStorageService.addBook(book4);
		bookStorageService.removeBookById(2);
		claimStorageService.submitClaim(claim);
		Order order = new Order(1);
		orderStorageService.addBookToOrder(order, book1);
		orderStorageService.addBookToOrder(order, book2);
		orderStorageService.addOrder(order);
		
		
		bookStorageService.sortBooksByAlphabet(bookStorage.getBooks());
		bookStorageService.sortBooksByPrice(bookStorage.getBooks());
		bookStorageService.sortUnsoldBooksByReceiptDate(bookStorage.getBooks());
		
		String TEST_FILE = s[0];
		String TEST_FILE2 = s[1];
		String TEST_FILE3 = s[2];
		try {
			BookStorageFile bookStorageFile = new BookStorageFile(bookStorage,
					TEST_FILE);
			OrderStorageFile orderStorageFile = new OrderStorageFile(orderStorage,TEST_FILE2);
			
			ClaimStorageFile claimStorageFile = new ClaimStorageFile(claimStorage,TEST_FILE3);

			orderStorageFile.saveOrderToFile();

			bookStorageFile.saveBooksToFile();
			claimStorageFile.saveClaimsToFile();
			List<Book> readedValues = bookStorageFile.readBooksFromFile();
			
			List<Order> readedOrders = orderStorageFile.readOrdersFromFile();
			
			List <Claim> readedClaims = claimStorageFile.readClaimsFromFile();
		
			
			for (int j = 0; j < orderStorage.getOrders().size(); j++) {

				if (!orderStorage.getOrders().get(j).toString()
						.equals(readedOrders.get(j).toString())) {
					throw new RuntimeException("Error. Not equal values2: "
							+ readedOrders.get(j) + " and "
							+ orderStorage.getOrders().get(j));

				}

			}
			
			for (int j = 0; j < claimStorage.getClaims().size(); j++) {

				if (!claimStorage.getClaims().get(j).toString()
						.equals(readedClaims.get(j).toString())) {
					throw new RuntimeException("Error. Not equal values3: "
							+ readedClaims.get(j) + " and "
							+ claimStorage.getClaims().get(j));

				}

			}


		}
		catch(ArrayIndexOutOfBoundsException e) {
			System.out.println("Change Main method arguments");
			
		}
		catch(Exception e) {
			
			
		}
		
		finally {

			//Files.deleteIfExists( Paths.get(TEST_FILE));
			//Files.deleteIfExists( Paths.get(TEST_FILE2));
			//Files.deleteIfExists( Paths.get(TEST_FILE3));
		}
	}
}
